package com.stox.shared.indicator.model;

import java.util.List;

import com.stox.shared.core.i18n.MessageSource;
import com.stox.shared.core.model.Bar;
import com.stox.shared.core.model.enumeration.BarValue;
import com.stox.shared.indicator.model.config.BarValueAwareConfig;
import com.stox.shared.indicator.model.intf.Indicator;

public abstract class AbstractIndicator<OUT, CONFIG> implements Indicator<OUT, CONFIG> {

	private final String code;

	public AbstractIndicator(final String code) {
		this.code = code;
	}

	@Override
	public String getCode() {
		return code;
	}

	@Override
	public String getDisplayName() {
		return MessageSource.get(code);
	}

	@Override
	public OUT getValue(final CONFIG configuration, final List<Bar> in, final int index) {
		return getValue(configuration, asDoubleArray(configuration, in), index);
	}

	@Override
	public OUT[] getValues(final CONFIG configuration, final List<Bar> in) {
		return getValues(configuration, asDoubleArray(configuration, in));
	}

	private Double[] asDoubleArray(final CONFIG configuration, final List<Bar> in) {
		BarValue barValue = BarValue.CLOSE;
		if (configuration instanceof BarValueAwareConfig) {
			final BarValueAwareConfig barValueAwareConfig = (BarValueAwareConfig) configuration;
			barValue = barValueAwareConfig.getBarValue();
		}
		final Double[] values = new Double[in.size()];
		for (int index = 0; index < in.size(); index++) {
			final Bar bar = in.get(index);
			values[index] = barValue.get(bar);
		}
		return values;
	}

}
